//
//  GestureViewController.swift
//  DemoProject
//
//  Created by hao on 2018/7/19.
//  Copyright © 2018年 hao. All rights reserved.
//

import UIKit

class GestureViewController: BaseTableViewController {

    let imageView = UIImageView(image: UIImage(named: "UIKit_Struct_Image"))
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupBackgroundImage()
        setupData()
        addGesture()
    }
    
    private func addGesture() {
        // 添加旋转手势
        let rotation = UIRotationGestureRecognizer()
        imageView.addGestureRecognizer(rotation)
        
        rotation.rx.event.subscribe(onNext: { recognizer in
            //旋转的弧度转换为角度
            print(info: recognizer.rotation*(180/CGFloat.pi))
//            kRadianToDegrees(recognizer.rotation)
        }).disposed(by: disposeBag)
        
        // 添加缩放手势
        let pinch = UIPinchGestureRecognizer()
        imageView.addGestureRecognizer(pinch)
        pinch.rx.event.bind { [weak self] recognizer in
            let height = self?.tableHeadView.height ?? 0
            guard let imageView = recognizer.view else { return }
            /// 控制缩放最小和最大
            if recognizer.scale < 1 {
                if imageView.width > MainScreenWidth()*0.5 {
                    imageView.width *= recognizer.scale
                    imageView.height *= recognizer.scale
                }
            } else {
                if imageView.height < height*3 {
                    imageView.width *= recognizer.scale
                    imageView.height *= recognizer.scale
                }
            }
            imageView.center = CGPoint(x: (self?.tableHeadView.width ?? 0)*0.5, y: height*0.5)
            print(info: "recognizer.scale: \(recognizer.scale)")
        }.disposed(by: disposeBag)
        
        // 拖动手势
        let pan = UIPanGestureRecognizer()
        imageView.addGestureRecognizer(pan)
        pan.rx.event.bind { [weak self] (recognizer) in
            guard let imageView = recognizer.view else { return }
            if recognizer.state == .ended || recognizer.state == .changed {
                if let headView = self?.tableHeadView {
                    let point = recognizer.translation(in: headView)
                    imageView.center = CGPoint(x: headView.width*0.5+point.x, y: headView.height*0.5+point.y)
                }
            }
//            imageView.center.x += point.x
//            imageView.center.y += point.y
        }.disposed(by: disposeBag)
    }
    
    private func setupData() {
        let titles = ["UISwipeGestureRecognizer",
                      "UIScreenEdgePanGestureRecognizer",
                      "UITapGestureRecognizer",
                      "UIPinchGestureRecognizer",
                      "UIRotationGestureRecognizer",
                      "UIPanGestureRecognizer",
                      "UILongPressGestureRecognizer"]
        
        let descArr = [
                       """
                        滑动（快速移动）
                        //表示监听滑动的方向为向上
                        swipe.direction = .up
                        如果 UISwipeGestureRecognizer 在不指定方向的时候，默认向右滑动才会触发事件。如果要指定方向，需要设置 direction 属性
                        如果各个方向都要响应，只要多定义几个 UISwipeGestureRecognizer 就可以
                       """,
                       """
                        （1）这个是 UISwipeGestureRecognizer 的子类，与后者不同的时。UIScreenEdgePanGestureRecognizer 只触发从边缘开始的划动操作，比如从左侧边缘开始向右滑动
                        （2）UIScreenEdgePanGestureRecognizer 没有 direction 属性，增加了个 edges 属性。表示要响应哪个边缘的划动操作。可以是从左边缘向右滑动，或右边缘向左、上边缘向下、下边缘向上、或响应全部边缘滑动
                       """,
                       """
                        轻点手势（点击）
                        （1）可以通过 numberOfTouchesRequired 属性设置触摸点数，比如设置 2 表示必须两个手指触摸时才会触发
                        （2）通过 numberOfTapsRequired 属性设置点击次数，单击设置为 1，双击设置为 2
                        （3）如果一个控件既监听了单击事件也监听了双击事件，默认当双击事件触发的时候也同时会触发单击事件。如果想双击时不触发单击，需要通过 require(toFail:) 进行设置
                       """,
                       """
                        捏合手势（两个手指进行放大缩小）
                       """,
                       """
                        旋转手势（两个手指进行旋转）
                       """,
                       """
                        拖动手势
                       """,
                       """
                        长按手势
                       """]
        
        addTitles(titles, footDescs: descArr)
    }

    private func setupBackgroundImage() {
        imageView.isUserInteractionEnabled = true
        self.tableHeadView.addSubview(imageView)
        self.tableHeadView.height *= 1.25
        imageView.frame = CGRect(x: 0, y: 0, width: self.tableHeadView.width, height: self.tableHeadView.height)
//        imageView.snp.makeConstraints { (make) in
//            make.top.left.right.bottom.equalTo(0)
//        }
    }
    
}
